package com.escool.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbCatMateriaTempDet;
import com.escool.model.TbGrado;
import com.escool.model.TbNivel;

import com.escool.model.TbCatMateriaTemp;


import com.escool.service.CatEventoService;
import com.escool.service.CatMateriaTempService;
import com.escool.service.GradosService;
import com.escool.service.NivelesService;
import com.escool.service.RolService;

@Controller
public class CatMateriaTempController {
	
	@Autowired
	CatMateriaTempService CatMateriaTempService;

	
	@RequestMapping("showCatMateriaTemp")
	public String showCatMateriaTemp(Map<String, Object> map){
		map.put("CatMateriaTemp", new TbCatMateriaTemp());
		map.put("TbCatMateriaTempList", CatMateriaTempService.listTbCatMateriaTemp());
		return "/catMateriaTemp";
	}
	
	@RequestMapping(value = "addCatMateriaTemp", method = RequestMethod.POST)
	public String addRoles(@ModelAttribute("CatMateriaTemp") @Valid TbCatMateriaTemp tbCatMateriaTemp, BindingResult result){
		if (!result.hasErrors()) {
			CatMateriaTempService.addTbCatMateriaTemp(tbCatMateriaTemp);
			return "redirect:/showCatMateriaTemp";
		}
		return "";
	}
	
	@RequestMapping(value = "editCatMateriaTemp", method = RequestMethod.POST)
	public String editRoles(@ModelAttribute("CatMateriaTemp") @Valid TbCatMateriaTemp tbCatMateriaTemp, BindingResult result){
		if (!result.hasErrors()) {
			CatMateriaTempService.editTbCatMateriaTemp(tbCatMateriaTemp);
			return "redirect:/showCatMateriaTemp";
		}
		return "";
	}
	
	@RequestMapping(value = "deleteCatMateriaTemp", method = RequestMethod.POST)
	public String delRoles(@ModelAttribute("CatMateriaTemp") @Valid TbCatMateriaTemp tbCatMateriaTemp, BindingResult result){
		if (!result.hasErrors()) {
			CatMateriaTempService.delTbCatMateriaTemp(tbCatMateriaTemp.getIdMateriaTemp());
			return "redirect:/showCatMateriaTemp";
		}
		return "";
	}
	
	@RequestMapping(value = "mostrarMaterias", method = RequestMethod.POST)
	public String mostrarMaterias(Map<String, Object> map,@ModelAttribute("CatMateriaTemp") @Valid TbCatMateriaTemp tbCatMateriaTemp, BindingResult result){
		TbCatMateriaTempDet tbCatMateriaTempDet = new TbCatMateriaTempDet();
		tbCatMateriaTempDet.setTbCatMateriaTemp(tbCatMateriaTemp);
		map.put("tbCatMateriaTempDet", tbCatMateriaTempDet);
		map.put("tbCatMateriaTempList", CatMateriaTempService.listTbCatMateriaTempDet(tbCatMateriaTemp.getIdMateriaTemp()));
		return "/showMateriaDet";

	}
	
	
	@RequestMapping("showtbCatMateriaTempDet")
	public String showtbCatMateriaTempDet(Map<String, Object> map,HttpServletRequest request){
		String id = request.getParameter("idColegio");		
		TbCatMateriaTempDet tbCatMateriaTempDet = new TbCatMateriaTempDet();
		tbCatMateriaTempDet.setTbCatMateriaTemp(CatMateriaTempService.findTbCatMateriaTemp(Integer.parseInt(id)));
		map.put("tbCatMateriaTempDet", tbCatMateriaTempDet);
		map.put("tbCatMateriaTempList", CatMateriaTempService.listTbCatMateriaTempDet(Integer.parseInt(id)));
		return "/showMateriaDet";
	}
	

	@RequestMapping(value = "addtbCatMateriaTempDet", method = RequestMethod.POST)
	public String addtbCatMateriaTempDet(@ModelAttribute("tbCatMateriaTempDet") @Valid TbCatMateriaTempDet TbCatMateriaTempDet, BindingResult result){
		if (!result.hasErrors()) {
			try{
				CatMateriaTempService.addTbCatMateriaTempDet(TbCatMateriaTempDet);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showtbCatMateriaTempDet?idColegio="+TbCatMateriaTempDet.getTbCatMateriaTemp().getIdMateriaTemp();
		}
		return "";
	}
	
	@RequestMapping(value = "edittbCatMateriaTempDet", method = RequestMethod.POST)
	public String edittbCatMateriaTempDet(@ModelAttribute("tbCatMateriaTempDet") @Valid TbCatMateriaTempDet TbCatMateriaTempDet, BindingResult result){
		if (!result.hasErrors()) {
			
			try{
				CatMateriaTempService.editTbCatMateriaTempDet(TbCatMateriaTempDet);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showtbCatMateriaTempDet?idColegio="+TbCatMateriaTempDet.getTbCatMateriaTemp().getIdMateriaTemp();
		}
		return "";
	}
	
	@RequestMapping(value = "deletetbCatMateriaTempDet", method = RequestMethod.POST)
	public String deltbCatMateriaTempDet(@ModelAttribute("tbCatMateriaTempDet") @Valid TbCatMateriaTempDet TbCatMateriaTempDet, BindingResult result){
		if (!result.hasErrors()) {
			try{
				CatMateriaTempService.delTbCatMateriaTempDet(TbCatMateriaTempDet.getIdCatMateriaTempDet());
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showtbCatMateriaTempDet?idColegio="+TbCatMateriaTempDet.getTbCatMateriaTemp().getIdMateriaTemp();
		}
		return "";
	}

}
